package com.quarters.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.quarters.bean.Quarters;
import com.quarters.bean.User;
import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.ztree.ZtreeBean;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 个人信息 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Controller
@RequestMapping("admin/user")
public class UserController extends BaseController {



    /**
     * @return 支付成功
     */
    @RequestMapping(value = "successful_payment.html",method = RequestMethod.GET)
    public String successful_payment(){
        return "wx/successful_payment";
    }

    @RequestMapping(value = "abc.html",method = RequestMethod.GET)
    public String abc(){
        return "wx/make";
    }

    /**
     * @return 支付失败
     */
    @RequestMapping(value = "error_payment.html",method = RequestMethod.GET)
    public String error_payment(){
        return "wx/error_payment";
    }

    @RequestMapping(value = "user_add.html",method = RequestMethod.GET)
    public String user_add(Model model){
        model.addAttribute("user",userService.list());
        model.addAttribute("quarters",quartersService.list());
        return "/admin/user/user_add";
    }

    @RequestMapping(value = "user_edit.html",method = RequestMethod.GET)
    public String user_edit(Model model,Long id){
        model.addAttribute("id",id);
        return "/admin/user/user_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<User> selectMyPage(VoPage voPage,HttpSession session,Integer status){
        if(getUserInfo(session).getUsername().equals("admin")){
            return userService.selectMyPageAll(voPage,status);
        }
        return userService.selectMyPage(voPage,getUserInfo(session).getId(),status);
    }

    /**
     * @param
     * @return 发送文章查询当前用户下小区的用户
     */
    @RequestMapping(value = "articleUser",method = RequestMethod.POST)
    @ResponseBody
    public List<ZtreeBean> articleUser(HttpSession session){
        //如果是超级管理员查询所有用户 否则就根据当前用户查询
        if(getUserInfo(session).getUsername().equals("admin")){
            return userService.articleUserAll();
        }
        return userService.articleUser(getUserInfo(session).getId());
    }


    /**
     * @param
     * @return 添加
     */
    @RequestMapping(value = "updateByIdUser",method = RequestMethod.POST)
    @ResponseBody
    public Integer updateByIdUser(Long userId,Long quartersId,String detailedFloor){
        User user = userService.getById(userId);
        if(user.getQuartersId()!=null && user.getDetailedFloor()!=null){
            return 2;
        }else{
            Long time = System.currentTimeMillis();
            return userService.updateByIdUser(userId,quartersId,detailedFloor,time);
        }
    }

    /**
     * @param
     * @return 修改
     */
    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    @ResponseBody
    public Map updateUser(@RequestBody User user){
        user.setUpdateTime(System.currentTimeMillis());
        if(userService.updateById(user)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }
    /**
     * @param user
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdUser",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteByIdUser(@RequestBody User user){
        if(userService.removeById(user)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    @RequestMapping(value = "getByIdUser",method = RequestMethod.GET)
    @ResponseBody
    public List getByIdUser(Long id){
        User user = userService.getById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",user.getQuartersId());
        Quarters quarters = quartersService.getOne(queryWrapper);
        List list = new ArrayList();
        list.add(user);
        list.add(quarters);
        return list;
    }


    /**
     * @return 查询所有
     */
    @RequestMapping(value = "selectList",method = RequestMethod.GET)
    @ResponseBody
    public List selectList(){
        return userService.list();
    }


    /**
     * @param session
     * @return 根据当前用户查询当前用户所管理的小区的所有用户
     */
    @RequestMapping(value = "selectUser",method = RequestMethod.GET)
    @ResponseBody
    public List selectUser(HttpSession session){
        //如果是admin就查询所有用户,否则就按条件查询
        if(getUserInfo(session).getUsername().equals("admin")){
            return userService.selectUserAll();
        }
        return userService.selectUser(getUserInfo(session).getId());
    }




}
